package com.codeavatar.catools.lib.sign;
/*
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2021
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 */

import android.text.TextUtils;

import com.codeavatar.catools.config.AppConfig;
import com.codeavatar.catools.config.ConstConfig;
import com.codeavatar.catools.helper.CaGsonHelper;
import com.codeavatar.catools.security.alipaySign.AlipaySign;
import com.codeavatar.catools.security.md5sign.Md5Sign;

import java.util.Map;

public class SignLib {

    private static final Object mLock = new Object();

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++

    /**
     * 验签
     *
     * @param map         含签名的数据
     * @param signKeyName 默认为null，使用APICore框架的 fwSign 键名
     * @param type        {@link CaSignType}
     * @param secretKey   默认为null
     * @return
     */
    public boolean checkSign(Map<String, String> map, String signKeyName, CaSignType type, String
            secretKey) {
        if (null == map)
            return false;

        boolean result = false;
        String signStr = "";
        if (!TextUtils.isEmpty(signKeyName)) {
            signStr = map.get(signKeyName);
            map.remove(signKeyName);
        } else {
            signStr = map.get(ConstConfig.API_KEY_SIGN);
            map.remove(ConstConfig.API_KEY_SIGN);
        }

        switch (type) {
            case Md5:
                if (TextUtils.isEmpty(secretKey)) {
                    secretKey = AppConfig.SIGN_KEY;
                }
                result = Md5Sign.checkSign(map, signStr, secretKey);
                break;
            case Rsa:
                if (TextUtils.isEmpty(secretKey)) {
                    secretKey = AppConfig.SIGN_KEY;
                }
                result = AlipaySign.checkSign(map, signStr, secretKey, false);
                break;
            case Rsa2:
                if (TextUtils.isEmpty(secretKey)) {
                    secretKey = AppConfig.SIGN_KEY;
                }
                result = AlipaySign.checkSign(map, signStr, secretKey, true);
                break;
        }
        return result;
    }

    /**
     * 加签
     *
     * @param map       不含签名的数据
     * @param type      {@link CaSignType}
     * @param secretKey 默认为null
     * @return
     */
    public String addSign(Map<String, String> map, CaSignType type, String secretKey) {

        if (null == map)
            return "";

        String result = "";
        switch (type) {
            case Md5:
                if (TextUtils.isEmpty(secretKey)) {
                    secretKey = AppConfig.SIGN_KEY;
                }
                result = Md5Sign.addSign(map, secretKey);
                break;
            case Rsa:
                if (TextUtils.isEmpty(secretKey)) {
                    secretKey = AppConfig.SIGN_KEY;
                }
                result = AlipaySign.getSign(map, secretKey, false);
                break;
            case Rsa2:
                if (TextUtils.isEmpty(secretKey)) {
                    secretKey = AppConfig.SIGN_KEY;
                }
                result = AlipaySign.getSign(map, secretKey, true);
                break;
        }
        return result;
    }

    private static SignLib init;
    public static SignLib init(){
        if(null == init){
            synchronized (mLock) {
                if (null == init) {
                    init = new SignLib();
                }
            }
        }
        return init;
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private SignLib(){
        // Private constructor to prevent unwanted construction.
    }
}
